Problem with function calls [javascript]

Posted by Samuel on Stack Overflow See other posts from Stack Overflow or by Samuel
Published on 2010-05-24T09:46:57Z Indexed on 2010/05/24 9:51 UTC
Read the original article Hit count: 218

Filed under:
|
<script language="javascript">
function toggle(id) {
    alert('call');
    if (document.getElementById(id).style.display == "none") {
        alert('now visible');
        document.getElementById(id).style.display = "";
    } else {
        alert('now invisible');
        document.getElementById(id).style.display = "none";
    }
}
</script>

</head>
<body onload="toggle('image1');alert('test_body');toggle('image2')">

<script language="javascript">
alert('test_pre_function');
toggle('image1');
alert('test_after_function');
toggle('image2');
</script>

Looks like a lot of code but it's pretty simple so i think most of you won't have troubles with it. toggle() should toggle the display status of divs containing images.

When the user enters the site the divs should hide, when everything is loaded the divs should show up. (onload)

Strangely enough, the funtion in the body (not in the body tag) only work half, i get and alert 'test_pre_function' and i get an alert 'call' (out of the function), but that's it. The code in the body tag runs just fine.

I find this weird because it's supposed to do exactly the same twice and one time it runs, another time not, so i guess i must have made some stupid mistake.

Thanks for any help!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about function-calls